<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

var docReceiveUuid;
var docReceiverOrgCode;
var receiverType = "${receiverType}";
$(function(){
	//隐藏签收和拒收按钮
	changeButton1();
	
	//弹出框设置
	$("#dialogDiv").dialog({
		title:"公文信息",//标题
		width: 800,//宽度
	    height: 340,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    buttons:[
	         <c:if test="${canOperate eq 'newcome'}">
	         {
				text:'签收',
				handler:function(){
					$.messager.confirm('提示',"您确定要签收此文件吗?",function(r){
						if(r){
							$.ajax({
								type:"POST",
								url:'<c:url value="/receive/signIn"/>',
								data: "docReceiveUuid="+docReceiveUuid+"&docReceiverOrgCode="+docReceiverOrgCode+"&receiveInfo="+$("#receiveInfo").textbox('getValue')+'&receiverType='+'<c:out value="${receiverType}"/>',
								success:function(msg){
								   if(msg.state=="200"){
//									   	var formId=msg.docFormId;
//										var issueId=msg.docId;
//										if(formId!=""&&issueId!=""){
//											window.location.href=_synergyContext+"/basic/flowItem/"+formId+"/isNew?issueId="+issueId;
//										}else{										
											$('#dialogDiv').dialog( "close" );
										    $.messager.alert('提示',"签收成功");
										    $('#synFromTable').datagrid({ queryParams:getParams()});
//										}								   
								   }else{
									   $.messager.alert('提示',"签收失败");
								   }										
								}
							});
						}
					});
				}
			},
	         {
				text:'拒收',
				handler:function(){
					$("#newcome").dialog("open");
				}
			},
			</c:if>
			{
				text:'关闭',
				handler:function(){$('#dialogDiv').dialog("close");}
			}
		],
	    onClose: function() {
			 $("#dialogIframe").attr("src","");
			} 
	});	
	
	//查询按钮绑定点击事件
	$("#searchButton").click(function(){
		$('#synFromTable').datagrid({ queryParams:getParams()});
	});
	
	$("#changeButton").click(function(){
		if($("input[name='docReceiveUuid']:checked").size()==1){			
			$.messager.confirm('提示', '您确定要转换此文件吗?', function(r){
				if (r){
					var $checked = $(':radio:checked');
					var $tr = $checked.closest('tr');
					docReceiveUuid = $tr.find(':radio').val();
					docReceiverOrgCode = $tr.find('td').eq(7).text();
					$.ajax({
						type:"POST",
						url:'<c:url value="/receive/changeType"/>', 
						data: "docReceiveUuid="+docReceiveUuid+"&docReceiverOrgCode="+docReceiverOrgCode+"&receiverType="+"${receiverType=='2'?'3':'2'}",
						success:function(msg){
						   if(msg.state=="200"){
							    $.messager.alert('提示',"转换成功");
							    $('#synFromTable').datagrid({ queryParams:getParams()});
						   }else{
							   $.messager.alert('提示',"转换失败");
						   }										
						}
					});
				}
			});
		}else{
			$.messager.alert('提示','请选择一条数据再转换');
		}
	});
	
	//签收按钮绑定点击事件
	$("#signInButton").click(function(){
		if($("input[name='docReceiveUuid']:checked").size()==1){			
			$.messager.confirm('提示', '您确定要签收此文件吗?', function(r){
				if (r){
					var $checked = $(':radio:checked');
					var $tr = $checked.closest('tr');
					docReceiveUuid = $tr.find(':radio').val();
					docReceiverOrgCode = $tr.find('td').eq(7).text();
					$.blockUI({ 
						message: '<img src="<c:url value="/images/busy.gif"/>" />&nbsp;&nbsp;<span>正在执行操作 请稍候...</span>', 
						css:{color: '#E17009',fontSize:'17px',padding:'10px'},
						onBlock:function(){
							$.ajax({
								type:"POST",
								url:'<c:url value="/receive/signIn"/>',
								data: "docReceiveUuid="+docReceiveUuid+"&docReceiverOrgCode="+docReceiverOrgCode+"&receiveInfo="+$("#receiveInfo").textbox('getValue')+'&receiverType='+'<c:out value="${receiverType}"/>',
								success:function(msg){
								   if(msg.state=="200"){
//									   	var formId=msg.docFormId;
//										var issueId=msg.docId;
//										if(formId!=""&&issueId!=""){
//											window.location.href=_synergyContext+"/basic/flowItem/"+formId+"/isNew?issueId="+issueId;
//										}else{
									   $.unblockUI();
									   $('#dialogDiv').dialog( "close" );
									   $.messager.alert('提示',"签收成功");
									   $('#synFromTable').datagrid({ queryParams:getParams()});
//										}								   
								   }else{
									   $.messager.alert('提示',"签收失败");
								   }										
								}
							});
						}
					});	
					
				}
			});
		}else{
			$.messager.alert('提示','请选择一条数据再签收');
		}
	});
	
	//拒收按钮绑定点击事件
	$("#refusedButton").click(function(){
		if($("input[name='docReceiveUuid']:checked").size()==1){			
			var $checked = $(':radio:checked');
			var $tr = $checked.closest('tr');
			docReceiveUuid = $tr.find(':radio').val();
			docReceiverOrgCode = $tr.find('td').eq(7).text();
			$("#newcome").dialog("open");
		}else{
			$.messager.alert('提示','请选择一条数据再拒收');
		}
	});
	
	//删除按钮绑定点击事件
	$("#deleteButton").click(function(){
		if($("input[name='docReceiveUuid']:checked").size()==1){			
			$.messager.confirm('提示', '确定要删除数据吗?', function(r){
				if (r){
					var $checked = $(':radio:checked');
					var $tr = $checked.closest('tr');
					var docReceiverOrgCode=$tr.find('td:eq(7)').text();
					$.ajax({
						   type: "POST",
						   url: "<c:url value='/receive/updateDocReceiveState' />",
						   data: "docReceiveUuid="+$("input[name='docReceiveUuid']:checked").val()+"&docReceiverOrgCode=" + docReceiverOrgCode,
						   success: function(msg){
							   if(msg.state=="200"){
								   $.messager.alert('提示','数据删除成功');
								   $('#synFromTable').datagrid({ queryParams:getParams()});
							   }else{
								   $.messager.alert('提示',"数据删除失败");
							   }
						   }
						});
				}
			});
		}else{
			$.messager.alert('提示','请选择一条数据再删除');
		}
	});
		
	//表格加载
	$('#synFromTable').datagrid({
	    url:'<c:url value="/receive/listDtReceiveDocument"/>?canOperate='+'<c:out value="${canOperate}"/>'+'&receiveOrgType='+'<c:out value="${receiveOrgType}"/>'+'&receiverType='+'<c:out value="${receiverType}"/>',
	    queryParams:getParams(),//提交参数
	    toolbar:'#synFromToolbar',//表头工具栏
	    pagination:true,//显示分页  
	    pagePosition:'bottom',//分页栏出现位置
	    pageNumber:1,//初始页码
	    pageSize:10,//初始每页显示记录数量
	    singleSelect:true,
	    nowrap: false,//设置内容多时换行，false换行
	    columns:[[
			{field:'docReceiveUuid',title:'选择',formatter: function(value,row,index){
				return '<input name="docReceiveUuid" type="radio" value="'+value+'" />';
			},width:40},
			{field:'index',title:'序号',formatter: function(value,row,index){
				return (index+1);
			},width:40},
			{field:'docTitle',title:'公文标题',width:250},
			{field:'docNumber',title:'来文编号',width:160},
			{field:'docReceiverOrgName',title:'收文单位',width:160},
			{field:'docSenderOrgName',title:'来文单位',width:150},
			{field:'docSendTime',title:'发文时间',width:90,formatter: function(value,row,index){
				var fromDate="";				
				if(value){
					fromDate=dataFormatStr(new Date(value),"yyyy-MM-dd");
				}
				return fromDate;
			}},
			{field: "docReceiverOrgCode" ,hidden:"true"},
			{field: "dispatchOrgCode" ,hidden:"true"},
			{field:'docInfo',title:'公文信息',width:70,formatter: function(value,row,index){
				return "<a href='javascript:void(0)' onclick='openDocInfo(this)'>查看</a>";
			}}
	    ]]
	});
	
	$("#newcome").dialog({
		title:"公文拒收",//标题
		width: 500,//宽度
	    height: 290,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    onClose: function() {
			 $("#receiveInfo").textbox("setValue","");
			} ,
	 buttons:[{
		 text:'确认',
			handler:function(){
				if($("#reasonForm").form('validate')){
					$.ajax({
						   type: "POST",
						   url: '<c:url value="/receive/refused"/>',
						   data: "docReceiveUuid="+docReceiveUuid+"&docReceiverOrgCode="+docReceiverOrgCode+"&receiveInfo="+$("#receiveInfo").textbox('getValue')+'&receiverType='+'<c:out value="${receiverType}"/>',
						   success: function(msg){
							   if(msg.state=="200"){
								    $.messager.alert('提示',"拒收成功");
								   $("#newcome").dialog("close");
								   $("#dialogDiv").dialog("close");
								   $('#synFromTable').datagrid({ queryParams:getParams()});
							   }else{
								   $.messager.alert('提示',"拒收失败");
							   }
						   }
						});
				}
			}
	 },{
		 text:'取消',
			handler:function(){
				$("#newcome").dialog("close");
			}
	 }]
	    });
});

//关闭弹出框
function closeDialog(){
	$('#synFromTable').datagrid({ queryParams:getParams()});
	$('#dialogDiv').dialog('close');
}

//查看公文信息
function openDocInfo(obj){
	docReceiveUuid = $(obj).closest('tr').find(':radio').val();
	docReceiverOrgCode = $(obj).closest('tr').find('td').eq(7).text();
	$("#dialogIframe").attr("src","<c:url value='/receive/viewReceiveDocument'/>?docReceiveUuid="+docReceiveUuid);
	$('#dialogDiv').dialog('open').window('center');
	
	/*var vHeight=320;                          //弹出窗口的宽度; 
    var vWidth=800;                         //弹出窗口的高度; 
    //获得窗口的垂直位置 
    var iTop = (window.screen.availHeight - 30 - vHeight) / 2; 
    //获得窗口的水平位置 
    var iLeft = (window.screen.availWidth - 10 - vWidth) / 2; 
	var url="<c:url value='/receive/viewDocIssueInfo'/>?docId="+docId;
	var winFeatures = "height="+vHeight+"px,width="+vWidth+"px,top=" + iTop + ",left=" + iLeft + ",toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
	window.open(url,'_blank',winFeatures);	*/	
}

//查询条件生成方法
function getParams(){
	var jsonObj=new Object();
	if($('input[name="docTitle"]').val()!=""){
		jsonObj.docTitle=$('input[name="docTitle"]').val();
	}
	if($('input[name="docNumber"]').val()!=""){
		jsonObj.docNumber=$('input[name="docNumber"]').val();
	}
	if($('input[name="docSenderOrgName"]').val()!=""){
		jsonObj.docSenderOrgName=$('input[name="docSenderOrgName"]').val();
	}
	if($('input[name="docSendTime"]').val()!=""){
		jsonObj.docSendTime=$('input[name="docSendTime"]').val();
	}
	//jsonObj.canOperate='<c:out value="${canOperate}"/>';
	//jsonObj.receiveOrgType='<c:out value="${receiveOrgType}"/>';
	return jsonObj;
}

function changeButton1(){
	var changeButton1 = "${changeButton1}";
	if(changeButton1 == "hide"){
		$("#changeButton1").hide();
	}
}
